using System;
using Uber.Core.Model;
using Uber.Core.Repository;
using Uber.Core.Service;

namespace Uber.Service
{
    public class Comments : IComments
    {
        private readonly INotifications notifications;
        private readonly IRepo<Comment> commentRepo;

        public Comments(IRepo<Comment> commentRepo, INotifications notifications)
        {
            this.commentRepo = commentRepo;
            this.notifications = notifications;
        }

        public Comment Add(long postId, long ownerId, string body)
        {
            var c = commentRepo.Create();
            c.Body = body;
            c.PostId = postId;
            c.OwnerId = ownerId;
            c.CreationDate = DateTime.Now;
            commentRepo.Add(c);
            commentRepo.Save();
            notifications.NewCommentAdded(postId, ownerId, body, c.Id);
            return c;
        }

        public void Edit(long id, string body, long userId)
        {
            var c = commentRepo.Get(id);
            var oc = c.Body;
            c.Body = body;
            commentRepo.Save();
            notifications.CommentEdited(c.PostId, c.Id, body, oc, userId);
        }

        public Comment Get(long id)
        {
            return commentRepo.Get(id);
        }

        public void Delete(long id)
        {
            commentRepo.Delete(id);
            commentRepo.Save();
            notifications.CommentDeleted(id);
        }
    }
}